<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CuongDD5
 * Date: 5/8/13
 * Time: 10:20 AM
 * To change this template use File | Settings | File Templates.
 */
include 'mySQLConnect.php';

//xac dinh bao nhieu dong
$display = 10;
// tinh tong so trang can hien thi
if (isset($_POST['page']) && (int)$_POST['page']) {
    $page = $_POST['page'];

} else {
    //neu chua xac dinh, thi tim so trang
    $query = "SELECT COUNT(id) FROM account";
    $res = mysql_query($query);
    $rows = mysql_fetch_array($res) or die(mysql_error());
    $record = $rows[0];
    if ($record > $display) {
        $page = ceil($record / $display);
    } else {
        $page = 1;
    }
}

$start = (isset($_POST['start']) && (int)$_POST['start'] >= 0) ? $_POST['start'] : 0;

$sql3 = "SELECT * FROM account ORDER BY id ASC LIMIT $start, $display";

$result3 = mysql_query($sql3);
$count = 0;
while ($row3 = mysql_fetch_array($result3)) {
    $count++;
    if ($count == 1) {
        ?>
        <table id="rounded-corner" style="width: 100%">
        <thead>
        <tr>
            <th>Tài khoản</th>
            <th>Công Cụ</th>
        </tr>
        </thead>
        <tbody>
    <?php
    }
    ?>

    <tr>
    <td><?php echo $row3['username']; ?></td>
        <td><a href="#"
               onclick="showEditFormAccount(<?php echo $row3['id']; ?>, '<?php echo $row3['username']; ?>', '<?php echo $row3['password'] ?>' );return false;">
                <div class="edit-button">Sửa</div>
            </a>
            <a href="#" onclick="showConfirmDialog(<?php echo $row3['id']; ?>);return false;">
                <div class="delete-button">Xóa</div>
            </a></td>
    </tr>
<?php
}?>
    </tbody>
</table>
    <?php
if ($count == 0) {
    echo("<p style='text-align: center'> Chưa có tài khoản nào! </p>");
}

include 'mySQLClose.php';

if ($page > 1) { //neu can hien thi so trang
    echo("<div style='text-align: center'>");
    $next = $start + $display;
    $prev = $start - $display;
    $current = ($start / $display) + 1;

    $datastrPrv = 'start=' . $prev . '&page=' . $page;
    $datastrNext = "start=" . $next . "&page=" . $page;

    //Hien thi trang Previous
    if ($current != 1) {
        ?>
        &nbsp;&nbsp;<b><a href="#" onclick="readAccount('<?php echo $datastrPrv; ?>');">Previous</a></b>&nbsp;&nbsp;
    <?php
    }
    //Hien thi so link
    for ($i = 1; $i <= $page; $i++) {
        if ($current != $i) {
            $datastrPage = "start=" . ($display * ($i - 1)) . "&page=" . $page;
            ?>
            &nbsp;&nbsp;<b><a href="#" onclick="readAccount('<?php echo $datastrPage; ?>');"><?php echo $i ?></a></b>
        <?php
        } else {
            echo "&nbsp;&nbsp;<b>$i</b>&nbsp;&nbsp;";
        }
    } //End: FOR

    //Hien thi trang Next

    if ($current != $page) {
        ?>
        &nbsp;&nbsp;<b><a href="#" onclick="readAccount('<?php echo $datastrNext ?>');">Next</a></b>&nbsp;&nbsp;
    <?php
    }
    echo("</div>");
}
?>